<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>使用urlSearchParams解析或组装url查询参数</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    let query = `name=测试&age=18&order=asc&arr=["前端","后端"]`
    let params = new URLSearchParams(query)
    // 跟map的使用方法一样
    console.log(params)
    for (let [k, v] of params) {
      console.log(k, v)
    }
    // get()  获取值
    let name = params.get("name")
    console.log(name)
    // has()  判断某个值是否存在
    console.log(params.has("age"))
    // toString() 获取组装好的查询字符串
    console.log(params.toString())
    // set()  修改键值对
    params.set("age", 20)
    // append() 追加一组新的键值
    params.append("say", "hello")
    // delete() 删除键值
    params.delete("arr")
  };
</script>

</html>